﻿/********************************************************************
*                                                                   *
* Microsoft Windows Sidebar Gadget for NNTT.ORG                     *
* Released under the MIT license by Alexey Glukharev, Januar 2012.  *
*                                                                   *
********************************************************************/

function getSizeString(value, showVal, showDim) {
    showVal = showVal != undefined? showVal: true;
    showDim = showDim != undefined? showDim: true;
    
    // Получим размерность и число
    var pow = getSizePow(value);
    var ret = value / Math.pow(1024, pow);
    
    // Удалим незначащие нули в конце (и точку, если потребуется)
    ret = ret.toFixed(2).rtrim("0");
    if (ret.charAt(ret.length - 1) == ".") {
        ret = ret.substr(0, ret.length - 1);
    }
    return (showVal? ret: "") + ((showVal & showDim)? " ": "") + (showDim? size[pow]: "");
}

// Функция определяет размерность числового значения
// Возвращает степень числа 1024
function getSizePow(value) {
    var loop = 0;
    while (loop < size.length) {
        var tmp = value / Math.pow(1024, loop);
        if (Math.floor(tmp) > 1024) {
            loop++;
        } else {
            return loop;
        }
    }
    return size.length - 1;
}

String.prototype.rtrim = function (c) {
    return this.replace(new RegExp(c + "+$", "i"), "");
}

// Удаление пробелов
function trimLeft(str) {
    return str.replace(/^\s+/, '');
}

function trimRight(str) {
    return str.replace(/\s+$/, '');
}

String.prototype.trim = function () {
    return trimRight(trimLeft(this));
}

/*
    OWL JavaScript Utilities
    Oran Looney, April 2008
    
    This work is licensed under the Creative Commons Attribution 3.0 United
    States License. To view a copy of this license, visit
    http://creativecommons.org/licenses/by/3.0/us/ or send a letter to
    Creative Commons, 171 Second Street, Suite 300,
    San Francisco, California, 94105, USA.
*/
function copy(obj) {
    // JavaScript doesn't have a 'copy' function, because each class will best know
    // how to copy itself. However, it is possible to provide a function that suffices for
    // many object, particularly Object literals.  This `copy()` will perform a shallow
    // copy on core JavaScript objects and will probably work for most user-defined classes.
    //   This copies an object exactly, including it's internal prototype and value references.
    // Only properties that are directly attached to the source object are copied.
    // However, an object and a copy will not compare equal with == or ===.
    //   Also, while this works on core JavaScript types, it probably won't work on
    // DOM elements and other objects provided by the runtime environment.
    if (typeof obj !== 'object') {
        return obj;
        // non-object have value sematics, so obj is already a copy.
    } else {
        var value = obj.valueOf();
        if (obj != value) {
            // the object is a standard object wrapper for a native type, say String.
            // we can make a copy by instantiating a new object around the value.
            return new obj.constructor(value);
        } else {
            // ok, we have a normal object. If possible, we'll clone the original's prototype
            // (not the original) to get an empty object with the same prototype chain as
            // the original.  If just copy the instance properties.  Otherwise, we have to
            // copy the whole thing, property-by-property.
            if (obj instanceof obj.constructor && obj.constructor !== Object) {
                var c = clone(obj.constructor.prototype);
                
                // give the copy all the instance properties of obj.  It has the same
                // prototype as obj, so inherited properties are already there.
                for (var property in obj) {
                    if (obj.hasOwnProperty(property)) {
                        c[property] = obj[property];
                    }
                }
            } else {
                var c = {
                };
                for (var property in obj) c[property] = obj[property];
            }
            
            return c;
        }
    }
}

// This version of clone was inspired by the MochiKit clone function.
function Clone() {}
function clone(obj) {
    Clone.prototype = obj;
    return new Clone();
}